package il.ac.braude.sona.control;
import il.ac.braude.sona.constants.ButtonPermission;
import il.ac.braude.sona.constants.Mssg;
import il.ac.braude.sona.constants.P;
import il.ac.braude.sona.entities.ChangeRequest;
import il.ac.braude.sona.gui.infogui.RequsetInfoFrame;
import il.ac.braude.sona.ocsf.client.Client;
import il.ac.braude.sona.ocsf.client.ServerResponseListener;

import javax.swing.JOptionPane;


/**
 * The ExecutionController contains all methods to handle Execution leader actions
 *  
 * @author Ofek Ron
 * @author Avihay Avitan
 * @author Nitzan Inbar
 * @author Sasha Rozenthal
 */

public class ExecutionController{
	/**
	 * The stream to handle data going to the server.
	 */
	private RequsetInfoFrame  reqFram;

	/**
	 * When execution leader wants to confirm that modification completed, 
	 * approveModifications sends msg to server for updating modification completed
	 * 
	 * @param req - the change request involved
	 * @param requsetInfoFrame - the request info fram
	 */
	public void approveModifications(ChangeRequest req, RequsetInfoFrame requsetInfoFrame) {
		reqFram=requsetInfoFrame;
		try {
			Client c = Client.getInstance();
			Mssg mssg= new Mssg(ButtonPermission.ApproveModifications,req.getID());
			c.sendToServer(new ServerResponseListener() {
				@Override
				public void onServerResponse(Mssg mssg) {
					if (mssg.is(P.error)) {
						JOptionPane.showMessageDialog(null,"cant get decision from DB");
						return;
					}
					else {
						JOptionPane.showMessageDialog(reqFram,"Modifications approved!");
						reqFram.dispose();
						reqFram.dispose();
					}
				}
			},mssg);
		 
		} catch (Exception e) {
			JOptionPane.showMessageDialog(reqFram,"cant UPDATE DB");
		}
		
	}



}
